#ifndef MGGS__LINK
#define MGGS__LINK

#include <cstdlib>
#include <iostream>
#include "Node.h"


class LinkedList
{
  public:
    Node* head;
    LinkedList();
    ~LinkedList();
    int isempty();
    void append(Node & _node);
    void append(char* _str);
    void list() const;
    Node* find(char* _str) const; //return the address of the previous node of
                                 //the first node containing the _str,and return
                                 //NULL if it dose not exist.
   
    

};


LinkedList::LinkedList()
{
    head = NULL;
};


LinkedList::~LinkedList()
{
    if (head != NULL)
    {
	Node* p = head;
	while (p->get_next() != NULL)
	{
	    Node* t = p;
	    p = p->get_next();
	    delete t;
	}
	delete p;
    }
};

int LinkedList::isempty(){
  if (head == NULL)
    return 0;
  else
    return 1;
}

void LinkedList::append(Node& _node)
{
    Node* tail = head;
    Node* p = new Node;
    p->set_str(_node.get_str());
    p->set_num(1);
    p->set_next(NULL);
    if (tail != NULL)
    {
    while (tail->get_next() != NULL)
	tail = tail->get_next();
    tail->set_next(p);
    }
    else
	head = p;
};

void LinkedList::append(char* _str)
{
    Node* tail = head;
    Node* p = new Node;
    p->set_str(_str);
    p->set_num(1);
    p->set_next(NULL);
    if (tail != NULL)
    {
    while (tail->get_next() != NULL)
	tail = tail->get_next();
    tail->set_next(p);
    }
    else
	head = p;
};

void LinkedList::list() const
{
    Node* tail = head;
    if (tail != NULL)
    {
	while (tail->get_next() != NULL)
	{
	    std::cout << tail->get_str() << ": " << tail->get_num() << ", ";
	    tail = tail->get_next();
	}
	std::cout << tail->get_str() << ": " << tail->get_num() << std::endl;
    }
};


Node* LinkedList::find(char* _str) const
{
    Node* p = head;
    if (p == NULL) //Empty list
	return NULL;
    if (strcasecmp(p->get_str(), _str) == 0)
    {                                    
	return p;
    }
    while (p->get_next() != NULL)
    {
      if (strcasecmp(p->get_next()->get_str(), _str) == 0)
	return p->get_next();
      p = p->get_next();
    }
    return NULL;
}






#else
// DO NOTHING.
#endif
